package com.qxzn.module.mine.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.RobotDeviceListBean;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RobotListAdapter extends RecyclerView.Adapter<RobotListAdapter.ViewHolder> {

    private Activity activity;
    private List<RobotDeviceListBean.DataBean> dataList;
    private RobotDeviceListBean.DataBean item;
    private View.OnClickListener checkClick;

    public RobotListAdapter(Activity activity, List dataList) {
        this.activity = activity;
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_robot_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        item = dataList.get(position);
        holder.tvName.setText(item.getName());
        holder.tvDeviceId.setText("ID:" + item.getDevice_id());
        Glide.with(activity).load(HttpRequest.BASE_URL + item.getPhoto()).apply(new RequestOptions().placeholder(R.mipmap.icon_xiaopai).error(R.mipmap.icon_xiaopai)).into(holder.ivIcon);
        if ("1".equals(item.getCurrent())) {
            holder.ivRight.setImageResource(R.mipmap.icon_robot_check);
            holder.tvCurrent.setVisibility(View.VISIBLE);
        } else {
            holder.ivRight.setImageResource(R.mipmap.icon_robot_uncheck);
            holder.tvCurrent.setVisibility(View.INVISIBLE);
        }
        holder.ivRight.setTag(item);
        holder.ivRight.setOnClickListener(checkClick);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setCheckClick(View.OnClickListener checkClick) {
        this.checkClick = checkClick;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.iv_icon)
        ImageView ivIcon;
        @BindView(R2.id.iv_right)
        ImageView ivRight;
        @BindView(R2.id.tv_current)
        TextView tvCurrent;
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_device_id)
        TextView tvDeviceId;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
